import express from 'express';
import goodsTypesModel from "../../../mongo/models/goodsTypes"
import goodsModel from "../../../mongo/models/goods"
const router = express.Router();

// 新增商品标签
router.post("/xcx/goodsType/add", (req: any, res) => {
    const { name } = req.body
    if (!name) {
        return res.send({
            code: 400,
            msg: "请输入商品类型名称"
        })
    }
    goodsTypesModel.create({ name }).then(result => {
        res.send({
            code: 200,
            msg: "添加成功"
        })
    }).catch(err => {
        res.send({
            code: 400,
            msg: "添加失败"
        })
    })
})

// 新增商品
router.post("/xcx/goods/add", (req: any, res) => {
    const { name, type } = req.body
    if (!name || !type) {
        return res.send({
            code: 400,
            msg: "请输入商品完整信息"
        })
    }
    goodsModel.create({ name, type }).then(result => {
        res.send({
            code: 200,
            msg: "添加成功"
        })
    }).catch(err => {
        res.send({
            code: 400,
            msg: "添加失败"
        })
    })
})

// 查询商品
router.post("/xcx/goods/list", (req: any, res) => {
    const { type } = req.body

    goodsModel.find().populate({
        path: "type", select: "name -_id", match: { _id: type }
    }).then(result => {
        res.send({
            code: 200,
            msg: "查询成功",
            data: result
        })
    }).catch(err => {
        res.send({
            code: 400,
            msg: "查询失败"
        })
    })
})

export default router; 